146. LRU 缓存
为保证权益,题目请参考 146. LRU 缓存(From LeetCode).
解决方案1
CPP
C++
/*
* LeetCode 146
* [LRU]
* 哈希表和双向链表
*/
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <queue>
using namespace std;
// leetcode 146
struct Node {
int val;
Node *pre;
Node *next;
explicit Node(int val) {
this->val = val;
this->pre = nullptr;
this->next = nullptr;
}
};
class LRUCache {
public:
int capacity;
unordered_map<int, int> maps;
// for delete
Node *head;
Node *tail;
unordered_map<int, Node *> node_maps;
explicit LRUCache(int capacity) {
this->capacity = capacity;
this->head = new Node(0);
this->tail = this->head;
}
int get(int key) {
if (maps.find(key) != maps.end()) {
Node *tmp = this->node_maps[key];
if (tmp->next != nullptr) {
tmp->pre->next = tmp->next;
tmp->next->pre = tmp->pre;
this->tail->next = tmp;
tmp->pre = this->tail;
tmp->next = nullptr;
this->tail = tmp;
}
return maps[key];
}else{
return -1;
}
return -1;
}
void put(int key, int value) {
if (maps.find(key) != maps.end()) {
maps[key] = value;
Node *tmp = this->node_maps[key];
if (tmp->next != nullptr) {
tmp->pre->next = tmp->next;
tmp->next->pre = tmp->pre;
this->tail->next = tmp;
tmp->pre = this->tail;
tmp->next = nullptr;
this->tail = tmp;
}
} else {
if (maps.size() + 1 > this->capacity) {
maps.erase(this->head->next->val);
node_maps.erase(this->head->next->val);
Node *tmp = this->head;
this->head = this->head->next;
delete tmp;
}
Node *tmp = new Node(key);
this->tail->next = tmp;
tmp->pre = this->tail;
this->tail = tmp;
node_maps.insert(unordered_map<int, Node *>::value_type(key, tmp));
maps.insert(unordered_map<int, int>::value_type(key, value));
}
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
int main() {
LRUCache cache(2);
cache.put(1, 1);
cache.put(2, 2);
cout << cache.get(1) << endl; // 返回 1
cache.put(3, 3); // 该操作会使得密钥 2 作废
cout << cache.get(2) << endl; // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得密钥 1 作废
cout << cache.get(1) << endl; // 返回 -1 (未找到)
cout << cache.get(3) << endl; // 返回 3
cout << cache.get(4) << endl; // 返回 4
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114